package com.travel.travelServer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.travelServer.entity.OrderFruit;
import com.travel.travelServer.entity.TabOrder;
import com.travel.travelServer.mapper.*;
import com.travel.travelServer.service.OrderService;
import com.travel.travelServer.utils.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * (Order)表服务实现类
 *
 * @author makejava
 * @since 2024-03-01 15:39:17
 */
@Service("orderService")
public class OrderServiceImpl extends BaseService<OrderMapper, TabOrder> implements OrderService {

    @Resource
    private CarMapper carDao;

    @Resource
    private UserMapper userDao;

    @Resource
    private OrderFruitMapper orderFruitMapper;

    @Resource
    private OrderMapper orderDao;

    @Resource
    private GoodsMapper fruitDao;

    @Override
    public Page<TabOrder> page(Page<TabOrder> page, Map<String, Object> conditions) {
        Long userId = getCurrentUser().getId();
        LambdaQueryWrapper<TabOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TabOrder::getUserId, userId);
        Page<TabOrder> orderPage = super.page(page, queryWrapper);
        orderPage.getRecords().stream().forEach(order -> {
            if (Objects.nonNull(order.getUserId())){
                order.setUser(userDao.selectById(order.getUserId()));
            }
            LambdaQueryWrapper<OrderFruit> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OrderFruit::getOrderId, userId);
            List<OrderFruit> tabOrders = orderFruitMapper.selectList(wrapper);
            tabOrders.stream().forEach(orderFruit -> {
                orderFruit.setGoods(fruitDao.selectById(orderFruit.getFruitId()));
            });
            order.setOrderFruits(tabOrders);
        });
        return orderPage;
    }

    @Override
    public TabOrder getById(Long id) {
        TabOrder tabOrder = super.getById(id);
        if (Objects.nonNull(tabOrder.getUserId())){
            tabOrder.setUser(userDao.selectById(tabOrder.getUserId()));
        }
        return tabOrder;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(TabOrder pojo) {
        Long userId = getCurrentUser().getId();
        pojo.setUserId(userId);
        super.save(pojo);
        if (pojo.getList().size() > 0){
            pojo.getList().stream().forEach(car -> {
                OrderFruit orderFruit = new OrderFruit();
                orderFruit.setOrderId(pojo.getId());
                orderFruit.setFruitId(car.getFruitId());
                orderFruit.setNumber(car.getNumber());
                orderFruitMapper.insert(orderFruit);
                car.setStatus(1);
                carDao.updateById(car);
            });
        }
        return true;
    }

    @Override
    public Boolean update(TabOrder pojo) {
        return super.updateById(pojo);
    }

    @Override
    public Boolean removeOne(Long id) {
        return super.removeById(id);
    }

    @Override
    public Boolean batchRemove(List<Long> asList) {
        return super.removeByIds(asList);
    }
}

